package model;

import org.hibernate.Query;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import util.MD5Code;

/**
 * A data access object (DAO) providing persistence and search support for
 * Student entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see model.Student
 * @author MyEclipse Persistence Tools
 */

public class StudentDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(StudentDAO.class);
	// property constants
	public static final String STUNUMBER = "stunumber";
	public static final String STUNAME = "stuname";
	public static final String STUSEX = "stusex";
	public static final String STUEMAIL = "stuemail";
	public static final String STUTELEPHONE = "stutelephone";
	public static final String STUADDRESS = "stuaddress";
	public static final String STUSPECIAL_ID = "stuspecialId";
	public static final String STUPASSWORD = "stupassword";

	protected void initDao() {
		// do nothing
	}

	/**
	 * 验证登陆
	 * @param teacherusername
	 * @param teacherpassword
	 * @return
	 */
	public String loginValidate(String stunumber,String stupassword)
	{
		MD5Code m = new MD5Code();
		String hql="FROM Student AS s WHERE s.stunumber=:stunumber";
		Query q=super.getSession().createQuery(hql);
		q.setString("stunumber", stunumber);
		if(q.list().size()==0)
			return "UserNotExist";
		
		String hql1="FROM Student AS s WHERE s.stunumber=:stunumber and s.stupassword=:stupassword";
		Query q1=super.getSession().createQuery(hql1);
		q1.setString("stunumber", stunumber);
		q1.setString("stupassword", m.getMD5ofStr(stupassword));
		if(q1.list().size()==0)
			return "PasswordError";
		Student s = new Student();
		s =(Student) q1.list().get(0);
		return s.getId()+";"+s.getStuname();
	}
	
	/**
	 * 修改密码
	 *
	 */
	public void editPassword(String stunumber,String stupasswordnew)
	{
		MD5Code m = new MD5Code();
		String hql="FROM Student AS s WHERE s.stunumber=:stunumber";
		Query q=super.getSession().createQuery(hql);
		q.setString("stunumber", stunumber);
		Student s = new Student();
		s =(Student) q.list().get(0);
		int id=s.getId();
		System.out.println("该用户的id为："+id);
		System.out.println("该用户的id为："+stupasswordnew);
		String hql1="UPDATE Student AS s SET s.stupassword=:stupasswordnew WHERE s.id=:id";
		Query q1=super.getSession().createQuery(hql1);
		q1.setInteger("id", id);
		q1.setString("stupasswordnew", m.getMD5ofStr(stupasswordnew));
		q1.executeUpdate();
	}
	/**
	 * get student by id
	 * 
	 */
	public List getStudentById(String id)
	{
		String hql="FROM Student AS st," +
		"Special AS sp,Department AS de"+ 
		" WHERE st.stuspecialId=sp.id and sp.specialdepartmentId=de.id";
		String temp="";
		String clause=temp;
		
		if(id!=null&&!"".equals(id))
		{
			clause=clause+" and st.id=:id";
		}
		
		if(!clause.equals(temp))
		{
			hql=hql+" "+clause;
		}
		System.out.println(hql);
		Query q=super.getSession().createQuery(hql);
		if(id!=null&&!"".equals(id))
		{
			q.setInteger("id", Integer.parseInt(id));
		}
		System.out.println(hql);
		List searchlist=q.list();
		return searchlist;
	}
	
	public void deleteAllStudentInfo()
	{
		String hql="DELETE FROM Student AS s";
		Query q=super.getSession().createQuery(hql);
		q.executeUpdate();
	}
	
	//get all student info
	public List getAllStudentInfo()
	{
		String hql="FROM Student AS st," +
				"Special AS sp,Department AS de"+ 
				" WHERE st.stuspecialId=sp.id and sp.specialdepartmentId=de.id";
		Query q=super.getSession().createQuery(hql);
		List list = q.list();
		return list;
	}
	
	//按学生学号、姓名、性别搜索学生信息
	public List searchStudents(String stunumber,String stuname,String stusex,String stuspecialId,String specialdepartmentId)
	{
		String hql="FROM Student AS st," +
		"Special AS sp,Department AS de"+ 
		" WHERE st.stuspecialId=sp.id and sp.specialdepartmentId=de.id";
		String temp="";
		String clause=temp;
		
		if(stunumber!=null&&!"".equals(stunumber))
		{
			clause=clause+" and st.stunumber like :stunumber";
		}
		if(stuname!=null&&!"".equals(stuname))
		{
			clause=clause+" and st.stuname like :stuname";
		}
		if(stusex!=null&&!"".equals(stusex)&&!stusex.equals("0"))
		{
			clause=clause+" and st.stusex=:stusex";
		}
		if(stuspecialId!=null&&!"".equals(stuspecialId)&&!stuspecialId.equals("0"))
		{
			clause=clause+" and st.stuspecialId=:stuspecialId";
		}
		if(specialdepartmentId!=null&&!"".equals(specialdepartmentId)&&!specialdepartmentId.equals("0"))
		{
			clause=clause+" and de.id=:specialdepartmentId";
		}
		if(!clause.equals(temp))
		{
			hql=hql+" "+clause;
		}
		System.out.println(hql);
		Query q=super.getSession().createQuery(hql);
		if(stunumber!=null&&!"".equals(stunumber))
		{
			q.setString("stunumber", '%'+stunumber+'%');
		}
		if(stuname!=null&&!"".equals(stuname))
		{
			q.setString("stuname", '%'+stuname+'%');
		}
		if(stusex!=null&&!"".equals(stusex)&&!stusex.equals("0"))
		{
			q.setString("stusex", stusex);
		}
		if(stuspecialId!=null&&!"".equals(stuspecialId)&&!stuspecialId.equals("0"))
		{
			q.setInteger("stuspecialId", Integer.parseInt(stuspecialId));
		}
		if(specialdepartmentId!=null&&!"".equals(specialdepartmentId)&&!specialdepartmentId.equals("0"))
		{
			q.setInteger("specialdepartmentId", Integer.parseInt(specialdepartmentId));
		}
		System.out.println(hql);
		List searchlist=q.list();
		return searchlist;
	}
	
	
	public void save(Student transientInstance) {
		log.debug("saving Student instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Student persistentInstance) {
		log.debug("deleting Student instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Student findById(java.lang.Integer id) {
		log.debug("getting Student instance with id: " + id);
		try {
			Student instance = (Student) getHibernateTemplate().get(
					"model.Student", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Student instance) {
		log.debug("finding Student instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Student instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Student as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByStunumber(Object stunumber) {
		return findByProperty(STUNUMBER, stunumber);
	}

	public List findByStuname(Object stuname) {
		return findByProperty(STUNAME, stuname);
	}

	public List findByStusex(Object stusex) {
		return findByProperty(STUSEX, stusex);
	}

	public List findByStuemail(Object stuemail) {
		return findByProperty(STUEMAIL, stuemail);
	}

	public List findByStutelephone(Object stutelephone) {
		return findByProperty(STUTELEPHONE, stutelephone);
	}

	public List findByStuaddress(Object stuaddress) {
		return findByProperty(STUADDRESS, stuaddress);
	}

	public List findByStuspecialId(Object stuspecialId) {
		return findByProperty(STUSPECIAL_ID, stuspecialId);
	}

	public List findByStupassword(Object stupassword) {
		return findByProperty(STUPASSWORD, stupassword);
	}

	public List findAll() {
		log.debug("finding all Student instances");
		try {
			String queryString = "from Student";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Student merge(Student detachedInstance) {
		log.debug("merging Student instance");
		try {
			Student result = (Student) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Student instance) {
		log.debug("attaching dirty Student instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Student instance) {
		log.debug("attaching clean Student instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static StudentDAO getFromApplicationContext(ApplicationContext ctx) {
		return (StudentDAO) ctx.getBean("StudentDAO");
	}
}